use thiserror::Error;

#[derive(Debug, Error)]
pub enum CodecError {
    #[error("buffer too large ({0} bytes)")]
    OversizeBuf(usize),

    #[error("buffer of {0} bytes bad for use in {1}")]
    MissizeBuf(usize, &'static str),

    #[error("decode buffer contains {0} extra bytes")]
    DanglingInput(usize),

    #[error("invalid varint value {0}")]
    InvalidVarintVal(u64),

    #[error("invalid varint encoding {0:?}")]
    InvalidVarintEnc([u8; 4]),

    #[error("invalid varint tag")]
    InvalidVarintTag,

    #[error("invalid option tag")]
    InvalidOptionTag(u8),

    #[error("tried to read past end of buffer")]
    BufferOverrun,

    #[error("tried to read when at end of input")]
    EndOfInput,

    #[error("tried to parse malformed bytes as utf8 string")]
    InvalidUtf8,

    #[error("invalid {0} variant discriminator {1}")]
    InvalidEnumVariant(&'static str, u64),

    #[error("unimplemented")]
    Unimplemented,

    #[error("{0}")]
    Other(String),
}

impl CodecError {
    pub fn from_tostr<T: ToString>(t: &T) -> Self {
        Self::Other(t.to_string())
    }
}
